package wxapp

import (
	"context"
	"hotgo/internal/dao"
	"hotgo/internal/model/do"
	"hotgo/internal/model/entity"

	"github.com/gogf/gf/v2/os/gtime"
)

func (s *sWxapp) InsertWxappUser(ctx context.Context, openid, session_key string) (*entity.WxUser, error) {
	s.Lock()
	defer s.Unlock()
	theUser, exist, err := s.userIsExist(ctx, openid)
	if !exist {
		_, err = dao.WxUser.Ctx(ctx).Data(do.WxUser{
			Openid:     openid,
			SessionKey: session_key,
			CreateAt:   gtime.Now(),
			UpdateAt:   gtime.Now(),
			Status:     0,
		}).Insert()
		theUser = &entity.WxUser{
			Openid: openid,
		}

	}
	return theUser, err
}

func (s *sWxapp) SetUserPhone(ctx context.Context, openid, phone string) error {
	s.Lock()
	defer s.Unlock()
	_, exist, err := s.userIsExist(ctx, openid)
	if !exist {
		_, err = dao.WxUser.Ctx(ctx).Data(do.WxUser{
			Openid:   openid,
			Phone:    phone,
			CreateAt: gtime.Now(),
			UpdateAt: gtime.Now(),
			Status:   0,
		}).Insert()
	} else {
		_, err = dao.WxUser.Ctx(ctx).Data(dao.WxUser.Columns().Phone, phone).Where(dao.WxUser.Columns().Openid, openid).Update()
	}
	return err
}
